import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Editora {
	private int id;
	private String nome;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	private String email;
	
	public static void setC_InsereEditora(){
		try (Connection cn = ConnectionFactory.createConnection()){
			Scanner entrada = new Scanner(System.in);
			
			System.out.println("Digite o nome da Editora:");
			String nome = entrada.nextLine();
			
			System.out.println("Digite o email da Editora:");
			String email = entrada.nextLine();
			
			PreparedStatement ps = cn.prepareStatement("INSERT INTO Editora (nome, email) Values (?, ?)");
			ps.setString(1, nome);
			ps.setString(2, email);
			
			ps.execute();
			entrada.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
	
	public static void getR_ListaEditoras(){
		try (Connection cn = ConnectionFactory.createConnection()){
			PreparedStatement ps = cn.prepareStatement("SELECT * FROM Editora");
			
			ResultSet rs = ps.executeQuery();
			Editora e = new Editora();
			while (rs.next()) {
				e.setId(rs.getInt("id"));
				e.setNome(rs.getString("nome"));
				e.setEmail(rs.getString("email"));
				
				System.out.print("Id : " + e.getId() + " ");
				System.out.print("Nome : " + e.getNome() + "\t");
				System.out.println("Email : " + e.getNome());
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
	
	public static void setU_AtualizaEditora(int id, String nome, String email){
		try (Connection cn = ConnectionFactory.createConnection()){
			PreparedStatement ps = cn.prepareStatement("UPDATE Editora set nome = ?, email = ? where id = ?");
			ps.setInt(3, id);
			ps.setString(1, nome);
			ps.setString(2, email);
			ps.execute();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
	
	public static void setD_DeleteEditora(int id){
		try (Connection cn = ConnectionFactory.createConnection()){
			PreparedStatement ps = cn.prepareStatement("DELETE FROM Editora where id = ?");
			ps.setInt(1, id);
			ps.execute();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
}
